﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
//using Microsoft.Web.Mvc.Internal;
using System.Web.Routing;

namespace Htse.Blue.Helpers
{
	public static class HtmlLabelHelper
	{
		public static string Label(this HtmlHelper htmlHelper, string text, string formElementName, IDictionary<string, object> htmlAttributes)
		{
			TagBuilder builder = new TagBuilder("label");

			builder.MergeAttribute("for", formElementName);

			builder.MergeAttributes<string, object>(htmlAttributes);

			builder.SetInnerText(text);

			return builder.ToString(TagRenderMode.Normal);
		}

		public static string Label(this HtmlHelper htmlHelper, string text, string formElementName, object htmlAttributes)
		{
			return Label(htmlHelper, text, formElementName, ((IDictionary<string, object>)new RouteValueDictionary(htmlAttributes)));
		}

		public static string Label(this HtmlHelper htmlHelper, string text, string formElementName)
		{
			return Label(htmlHelper, text, formElementName, null);
		}

		public static string LabelFor<TModel, TProperty>(
			this HtmlHelper<TModel> htmlHelper,
			string text,
			Expression<Func<TModel, TProperty>> expression,
			IDictionary<string, object> htmlAttributes)
			where TModel : class
		{
			string inputName = ExpressionHelperExtension.GetInputName<TModel, TProperty>(expression); //ExpressionHelper.GetInputName<TModel, TProperty>(expression);
			return htmlHelper.Label(text, inputName, htmlAttributes);
		}

		public static string LabelFor<TModel, TProperty>(
			this HtmlHelper<TModel> htmlHelper,
			string text,
			Expression<Func<TModel, TProperty>> expression,
			object htmlAttributes)
			where TModel : class
		{
			return LabelFor(htmlHelper, text, expression, htmlAttributes);
		}

		public static string LabelFor<TModel, TProperty>(
			this HtmlHelper<TModel> htmlHelper,
			string text,
			Expression<Func<TModel, TProperty>> expression)
			where TModel : class
		{
			return LabelFor(htmlHelper, text, expression, null);
		}
	}
}
